package com.zhoug.zmui.menu.horizontal;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.zhoug.common.utils.LogUtils;
import com.zhoug.zmui.R;
import com.zhoug.zmui.ZMUITextView;
import com.zhoug.zmui.group.ZMUILineaLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 菜单按钮
 *
 * @Author 35574
 * @Date 2020/11/2
 * @Description
 */
public class ZMUIMenuView extends LinearLayout {
    private static final String TAG = ">>>ZMUIMenuView";
    private static final boolean DEBUG = false;
    private ZMUITextView mMenuText;
    private ZMUILineaLayout mBtn;

    private Drawable normalIcon;//正常时的图标
    private Drawable expandIcon;//菜单展开时的图标
    private int iconWidth;//图标大小
    private int iconHeight;//图标大小
    private int textColor;//正常时的文字颜色
    private float textSize;//文字大小
    private int expandTextColor;//展开时的文字颜色
    private int drawablePadding;//文字和图标之间的间隔
    private int maxWidth;
    private boolean expanded;//是否展开
    /**
     * 是否自动切换展开/关闭,
     * 设置为true,那么点击会切换expanded状态,
     * 设置为false,点击只会设置expanded状态为true,false状态需要显示调用{@link #setExpanded(boolean)}方法,这样可以防止重复点击
     */
    private boolean auto;

    private OnExpandChangeListener onExpandChangeListener;
    private String menuName;


    public ZMUIMenuView(@NonNull Context context) {
        this(context, null);
    }

    public ZMUIMenuView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZMUIMenuView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(LinearLayout.VERTICAL);
        setGravity(Gravity.CENTER_VERTICAL);
        inflateLayout();
        init(context, attrs, defStyleAttr);
    }

    private void inflateLayout() {
        View root = LayoutInflater.from(getContext()).inflate(R.layout.zmui_menu_view, this, false);
        addView(root);
        findViews();
        setClickable(true);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (auto) {
                    expanded = !expanded;
                    _setIcon();
                    _setTextColor();
                    if (onExpandChangeListener != null) {
                        onExpandChangeListener.onExpand(expanded);
                    }
                } else {
                    //未展开
                    if (!expanded) {
                        expanded = true;
                        _setIcon();
                        _setTextColor();
                        if (onExpandChangeListener != null) {
                            onExpandChangeListener.onExpand(expanded);
                        }
                    }
                }

            }
        });
    }

    private void findViews() {
        mMenuText = findViewById(R.id.menu_text);
        mBtn = findViewById(R.id.btn);
    }

    private void init(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZMUIMenuView, defStyleAttr, R.style.zmui_style_ZMUIMenuView);
        int indexCount = a.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int attr = a.getIndex(i);
            if (attr == R.styleable.ZMUIMenuView_zmui_icon_normal) {
                normalIcon = a.getDrawable(attr);
            } else if (attr == R.styleable.ZMUIMenuView_zmui_icon_expanded) {
                expandIcon = a.getDrawable(attr);
            } else if (attr == R.styleable.ZMUIMenuView_zmui_drawable_width) {
                iconWidth = a.getDimensionPixelSize(attr, 0);
            } else if (attr == R.styleable.ZMUIMenuView_zmui_drawable_height) {
                iconHeight = a.getDimensionPixelSize(attr, 0);
            } else if (attr == R.styleable.ZMUIMenuView_zmui_textColor) {
                textColor = a.getColor(attr, 0);
            } else if (attr == R.styleable.ZMUIMenuView_zmui_textSize) {
                textSize = a.getDimension(attr, 0);
            } else if (attr == R.styleable.ZMUIMenuView_zmui_textColor_expanded) {
                expandTextColor = a.getColor(attr, 0);
            } else if (attr == R.styleable.ZMUIMenuView_zmui_maxWidth) {
                maxWidth = a.getDimensionPixelOffset(attr, 0);
            } else if (attr == R.styleable.ZMUIMenuView_android_drawablePadding) {
                drawablePadding = a.getDimensionPixelSize(attr, 0);
            } else if (attr == R.styleable.ZMUIMenuView_android_text) {
                menuName = a.getString(attr);
            } else if (attr == R.styleable.ZMUIMenuView_android_maxEms) {
                int maxEms = a.getInt(attr, 0);
                if (maxEms != 0) {
                    mMenuText.setMaxEms(maxEms);
                }
            } else if (attr == R.styleable.ZMUIMenuView_zmui_auto) {
                auto = a.getBoolean(attr, false);
            }
        }
        a.recycle();
        _setIcon();
        _setIconSize();
        _setIconPadding();
        _setTextColor();
        _setTextSize();
        setMaxWidth(maxWidth);
        setMenuName(menuName);
    }


    /**
     * 设置图标
     */
    private void _setIcon() {
        if (mMenuText != null) {
            if (expandIcon == null) {
                mMenuText.setCompoundDrawables(null, null, normalIcon, null);
            } else {
                mMenuText.setCompoundDrawables(null, null, isExpanded() ? expandIcon : normalIcon, null);
            }
        }
    }

    /***
     * 设置图标大小
     */
    private void _setIconSize() {
        if (mMenuText != null) {
            mMenuText.setDrawableSize(iconWidth, iconHeight);
        }
    }

    private void _setIconPadding() {
        if (mMenuText != null) {
            mMenuText.setCompoundDrawablePadding(drawablePadding);
        }
    }

    private void _setTextColor() {
        if (mMenuText != null) {
            if (expandTextColor == 0) {
                mMenuText.setTextColor(textColor);
            } else {
                mMenuText.setTextColor(isExpanded() ? expandTextColor : textColor);
            }
        }
    }

    private void _setTextSize() {
        if (mMenuText != null) {
            mMenuText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        }
    }

    public void setMaxWidth(int maxWidth) {
        this.maxWidth = maxWidth;
        if (mBtn != null) {
            mBtn.setMaxWidth(maxWidth);
        }
    }

    public void setMenuName(String menuName) {
        this.menuName = menuName;
        if (mMenuText != null) {
            mMenuText.setText(menuName);
        } else {
            mMenuText.setText(null);
        }
        requestLayout();

    }

    public void setNormalIcon(Drawable normalIcon) {
        if (this.normalIcon != normalIcon) {
            this.normalIcon = normalIcon;
            _setIcon();
        }
    }

    public void setExpandIcon(Drawable expandIcon) {
        if (this.expandIcon != expandIcon) {
            this.expandIcon = expandIcon;
            _setIcon();
        }
    }

    public void setIconSize(int iconWidth, int iconHeight) {
        this.iconWidth = iconWidth;
        this.iconHeight = iconHeight;
        _setIconSize();
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
        _setTextColor();
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
        _setTextSize();
    }

    public void setExpandTextColor(int expandTextColor) {
        this.expandTextColor = expandTextColor;
        _setTextColor();
    }


    public boolean isExpanded() {
        return expanded;
    }

    public void setExpanded(boolean expanded) {
        this.expanded = expanded;
        _setTextColor();
        _setIcon();
    }

    public void setOnExpandChangeListener(OnExpandChangeListener onExpandChangeListener) {
        this.onExpandChangeListener = onExpandChangeListener;
    }

    /**
     * @Author 35574
     * @Date 2020/11/2
     * @Description
     */
    public interface OnExpandChangeListener {
        void onExpand(boolean expand);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (DEBUG) {
            LogUtils.d(TAG, "onMeasure:" + MeasureSpec.getSize(widthMeasureSpec));
        }
    }
}
